package com.eclink.asset.asset.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.asset.domain.dao.AssetItem;
import com.eclink.asset.asset.domain.dao.AssetMaintenance;
import com.eclink.asset.asset.domain.dto.AssetItemWithMaintenanceDTO;
import com.eclink.asset.asset.mapper.AssetMaintenanceMapper;
import com.eclink.asset.mapper.AssetItemMapper;
import com.eclink.asset.mapper.CommonUserMapper;
import com.eclink.asset.pojo.query.AssetItemQuery;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资产维修Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class AssetMaintenanceService {

    @Resource
    private AssetItemMapper assetItemMapper;

    @Resource
    private AssetMaintenanceMapper assetMaintenanceMapper;

    @Resource
    private CommonUserMapper userMapper;

    public Page<AssetItemWithMaintenanceDTO> selectList(AssetItemQuery query, Pageable page) {

        MPJLambdaWrapper<AssetItem> wrapper = JoinWrappers.lambda(AssetItem.class)
                .selectAll(AssetItem.class)
                .selectAssociation(AssetMaintenance.class, AssetItemWithMaintenanceDTO::getMaintenance)
                .leftJoin(AssetMaintenance.class, AssetMaintenance::getItemId, AssetItem::getId)
                .eq(AssetItem::getDelFlag, 0);

        if (query.getState()!=null) {
            wrapper.eq(AssetItem::getState, query.getState());
        }
        if (StringUtils.isNotBlank(query.getValue())) {
            wrapper.like(query.getField(),query.getValue());
        }
        wrapper.orderByDesc("id");
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        Page<AssetItemWithMaintenanceDTO> pageQuery = new Page<>(pageNumber, pageSize);

        Page<AssetItemWithMaintenanceDTO> pageData = assetItemMapper.selectJoinPage(pageQuery, AssetItemWithMaintenanceDTO.class, wrapper);
        for(AssetItemWithMaintenanceDTO dto : pageData.getRecords()) {
            AssetMaintenance maintenance = dto.getMaintenance();

            if (maintenance != null && maintenance.getUserId() != null) {
                String userName = userMapper.getNickNameById(maintenance.getUserId());
                maintenance.setUserName(userName);
            }
        }
        return pageData;
    }


    /**
     * 查询资产清单表
     *
     * @param id 资产清单表主键
     * @return 资产清单表
     */
    public AssetMaintenance selectById(Long id) {
        return assetMaintenanceMapper.selectById(id);
    }

    public AssetMaintenance getMaintenanceByItemId(Long itemId) {
        LambdaQueryWrapper<AssetMaintenance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AssetMaintenance::getItemId, itemId);
        wrapper.last("limit 1");
        return assetMaintenanceMapper.selectOne(wrapper);

    }

    /**
     * 保存维保信息
     *
     * @param entity 保存对象

     */
    public void save(Long itemId, AssetMaintenance entity) {

        AssetMaintenance maintenance = getMaintenanceByItemId(itemId);

        if (maintenance == null) {
            entity.setItemId(itemId);
            assetMaintenanceMapper.insert(entity);
        } else {
            BeanUtil.copyProperties(entity, maintenance);
            assetMaintenanceMapper.updateById(maintenance);
        }
    }

}
